package com.geeksaga.dao.ibatis.oracle;

import java.util.List;


import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.geeksaga.common.util.DateConvertor;
import com.geeksaga.common.util.StringUtils;
import com.geeksaga.dao.BbsViewLogDao;
import com.geeksaga.dao.ibatis.BaseIBatisDao;
import com.geeksaga.domain.Bbs;
import com.geeksaga.domain.BbsViewLog;

/**
 * @author newbie
 * @since 0.1
 */
@Repository
public class BbsViewLogDaoImpl extends BaseIBatisDao implements BbsViewLogDao {

	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsViewLogDao#add(com.geeksaga.domain.BbsViewLog)
	 */
	public BbsViewLog add(Bbs bbs) throws DataAccessException {
        BbsViewLog viewLog = new BbsViewLog();
        viewLog.setBbsSid(bbs.getBbsNo());
        viewLog.setRegistTimestamp(DateConvertor.getDateTimeFormat());
        viewLog.setUserSid(StringUtils.convertNulltoString(bbs.getUserSid(), ""));
        viewLog.setUserIp(bbs.getUserIp());
		
		return (BbsViewLog) getSqlMapClientTemplate().insert("BBS_VIEW_LOG.add", viewLog);
	}

	
	/* (non-Javadoc)
	 * @see com.geeksaga.dao.BbsViewLogDao#find(com.geeksaga.domain.Bbs)
	 */
	@SuppressWarnings("unchecked")
	public boolean exists(Bbs bbs) throws DataAccessException {
		bbs.setRegistDate(DateConvertor.getDateFormat());
		
		List<BbsViewLog> list = (List<BbsViewLog>) getSqlMapClientTemplate().queryForList("BBS_VIEW_LOG.find", bbs);
		
		if(list == null || (list != null && list.size() == 0))
		    return false;
		else
		    return true;
	}
}